﻿//1.引入express模块
var express = require('express');
var expressjwt = require('express-jwt').expressjwt
//导入cors包
const cors = require('cors')
//调用


//连接mongodb数据库
require("./db/index.js")
//2.创建express实例对象
var app = express();
//处理post参数
app.use(cors());
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
//设置中间件 验证token的有效性
app.use(expressjwt({
    secret: 'nodejs',
    algorithms: ['HS256']
}).unless({
    path: [ //接口白名单, 这里的接口时免除token验证的
        '/api/user/login',
        '/api/user/register',
        '/api/user/list',
        '/api/user/zhanshi',
        '/api/user/search',
        '/api/user/wenku',
        '/api/user/address',
        '/api/user/addlist',
        '/api/user/zhaohui',
        '/api/user/modefiy',
        '/api/user/addnews',
        '/api/user/newslist',
        '/api/user/dele',
        '/api/user/add',
        '/api/user/comlist',
        '/api/user/chakan',
        '/api/user/mohusearch',
        '/api/user/addcomments',
        '/api/user/commentslist',
        '/api/user/mycomments',
        '/api/user/delec'
    ]
}))
var userRouter = require('./router/user.js')
//应用 用户的 子路由
app.use("/api/user", userRouter)
//3.监听端口
app.listen(4000, () => {
    console.log('服务器运行在 http://localhost:4000');
});



